1030F - Putting Boxes Together - CodeForces Solution


data structures *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXSIZE=30000020;

int bufpos;

char buf[MAXSIZE];

#define NEG 1

void init(){

	#ifdef LOCAL

		freopen("1053C.txt","r",stdin);

	#endif

	buf[fread(buf,1,MAXSIZE,stdin)]='\0';

	bufpos=0;

}

#if NEG

int readint(){

	bool isneg;

	int val=0;

	for(;!isdigit(buf[bufpos]) && buf[bufpos]!='-';bufpos++);

	bufpos+=(isneg=buf[bufpos]=='-');

	for(;isdigit(buf[bufpos]);bufpos++)

		val=val*10+buf[bufpos]-'0';

	return isneg?-val:val;

}

#else

int readint(){

	int val=0;

	for(;!isdigit(buf[bufpos]);bufpos++);

	for(;isdigit(buf[bufpos]);bufpos++)

		val=val*10+buf[bufpos]-'0';

	return val;

}

#endif

char readchar(){

	for(;isspace(buf[bufpos]);bufpos++);

	return buf[bufpos++];

}

int readstr(char* s){

	int cur=0;

	for(;isspace(buf[bufpos]);bufpos++);

	for(;!isspace(buf[bufpos]);bufpos++)

		s[cur++]=buf[bufpos];

	s[cur]='\0';

	return cur;

}

const int maxn=200002;

const int mod=1000000007;

struct bit{

	int n;

	ll a[maxn];

	void add(int p,ll v){

		for(;p<=n;p+=p&-p)

			a[p]+=v;

	}

	ll query(int p){

		ll ans=0;

		for(;p;p-=p&-p)

			ans+=a[p];

		return ans;

	}

}b1,b2;

ll a[maxn],w[maxn];

int main(){

	init();

	int n=readint(),q=readint();

	for(int i=1;i<=n;i++)

		a[i]=readint()-i;

	b1.n=b2.n=n;

	for(int i=1;i<=n;i++){

		w[i]=readint();

		b1.add(i,w[i]);

		b2.add(i,w[i]*a[i]%mod);

	}

	while(q--){

		int x=readint(),y=readint();

		if (x<0){

			x=-x;

			b1.add(x,y-w[x]);

			b2.add(x,y*a[x]%mod-w[x]*a[x]%mod);

			w[x]=y;

			continue;

		}

		ll o=b1.query(x-1),t=b1.query(y);

		int l=x,r=y;

		while(l<r){ //first delta>0

			int mid=(l+r)/2; //on a[mid]

			if (2*b1.query(mid)-o-t>0)

				r=mid;

			else l=mid+1;

		}

		ll ans=(b1.query(l)-o)%mod*a[l]-b2.query(l)+b2.query(x-1)+b2.query(y)-b2.query(l)-(t-b1.query(l))%mod*a[l];

		printf("%lld\n",(ans%mod+mod)%mod);

	}

}


Comments

Submit
0 Comments
More Questions

1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know